package com.example.eback.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;

@Service
public class FilePathToUrlService {

    private final HttpServletRequest request;

    @Autowired
    public FilePathToUrlService(HttpServletRequest request) {
        this.request = request;
    }

    public String convert(String filepath) {
        // 获取服务器的基础URL
        String serverBaseUrl = request.getRequestURL().toString();
        serverBaseUrl = serverBaseUrl.substring(0, serverBaseUrl.lastIndexOf("/"));

        // 构建文件的访问URL
        String fileUrl = serverBaseUrl + "/" + filepath;
        fileUrl = fileUrl.replace("\\", "/");
        return fileUrl;
    }
}

